package com.halloon.android.ui.fragment;

import java.util.ArrayList;

import android.app.Activity;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.AsyncTask;
import android.os.Build;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ListView;

import com.actionbarsherlock.app.SherlockFragment;
import com.halloon.android.R;
import com.halloon.android.adapter.LBSContactAdapter;
import com.halloon.android.bean.UserBean;
import com.halloon.android.data.ContentManager;
import com.halloon.android.task.LocationTask;
import com.halloon.android.util.Constants;

public class LBSContactFragment extends SherlockFragment implements OnClickListener{
	
	private LBSContactFragmentCallback lcCallback;
	private Context context;
	private ListView listView;
	private Button backButton;
	private LBSContactAdapter lbsAdapter;
	private ArrayList<UserBean> userBeans;
	
	private String longitude;
	private String latitude;
	
	private LocationTask locationTask;
	private MyBroadCastReceiver mBroadCast = null;
	
	public interface LBSContactFragmentCallback{
		public void destroyLBSFragment();
	}
	
	@Override
	public void onAttach(Activity activity){
		super.onAttach(activity);
		this.lcCallback = (LBSContactFragmentCallback) activity;
	}

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState){
		View root = inflater.inflate(R.layout.lbs_contacts, null, false);
		
		context = getActivity();
		
		listView = (ListView) root.findViewById(R.id.lbs_contacts);
		backButton = (Button) root.findViewById(R.id.back_button);
		backButton.setOnClickListener(this);
		userBeans = new ArrayList<UserBean>();
		lbsAdapter = new LBSContactAdapter(context, userBeans);
		listView.setAdapter(lbsAdapter);
		locationTask = new LocationTask(context);
		if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB){
			locationTask.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
		}else{
			locationTask.execute();
		}
		
		return root;
    }
	
	@Override
	public void onStart(){
		super.onStart();
		mBroadCast = new MyBroadCastReceiver();
		IntentFilter filter = new IntentFilter();
		filter.addAction(Constants.LOCATION_GOT_LOCATION);
		context.registerReceiver(mBroadCast, filter);
	}
	
	private void loadData(){
		AsyncTask<Void, Void, String> task = new AsyncTask<Void, Void, String>(){
			@Override
			protected void onPreExecute(){
				
			}
			
			@Override
			protected String doInBackground(Void... params){
				ArrayList<UserBean> temp_userBean = ContentManager.getInstance(context).getAroundPeople(longitude, latitude, "", "0");
				userBeans.addAll(temp_userBean);
				
				return null;
			}
			
			@Override
			protected void onPostExecute(String result){
				lbsAdapter.notifyDataSetChanged();
			}
		};
		if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB){
			task.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
		}else{
			task.execute();
		}
	}
	
	@Override
	public void onActivityCreated(Bundle savedInstanceState){
		super.onActivityCreated(savedInstanceState);
	}

	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		lcCallback.destroyLBSFragment();
	}
	
	@Override
	public void onStop(){
		super.onStop();
		if(locationTask != null) locationTask.cancel(true);
		context.unregisterReceiver(mBroadCast);
	}
	
	private class MyBroadCastReceiver extends BroadcastReceiver{

		@Override
		public void onReceive(Context context, Intent intent) {
			Bundle bundle = intent.getExtras();
			longitude = String.valueOf(bundle.getDouble("longitude"));
			latitude = String.valueOf(bundle.getDouble("latitude"));
			loadData();
		}
	}
}
